package com.beordie.version_2.beans.support;

import com.beordie.utils.StringUtil;
import com.beordie.version_2.beans.config.BODBeanDefinition;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Description
 * @Date 2022/2/14 8:46
 * @Created 30500
 */
public class BODBeanDefinitionReader {
    /**
     * 配置文件信息
     */
    private Properties contextConfig = new Properties();

    /**
     * 需要进行注册的 bean
     */
    private List<String> registerBeanClasses = new ArrayList<>();

    /**
     * 加载扫描配置文件
     * @param locations
     */
    public BODBeanDefinitionReader(String ... locations) {
        //  1.加载配置文件信息流
        String parameter = locations[0];
        try {
            InputStream stream = BODBeanDefinitionReader.class.getClassLoader().getResourceAsStream(parameter);
            this.contextConfig.load(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //  2.扫描相关类
        scanPackage(this.contextConfig.getProperty("base"));
    }

    /**
     * 创建映射 BeanDefinition 关系
     * @return
     */
    public List<BODBeanDefinition> loadBeanFefinitions() {
        List<BODBeanDefinition> result = new ArrayList<>();
        try {
            for (String className : this.registerBeanClasses) {
                Class<?> beanClass = Class.forName(className);
                //  是接口就跳过
                if (beanClass.isInterface()) { continue; }

                //  1.默认首字母小写为映射名
                result.add(createBeanDefinition(StringUtil.lowerCaseFisrt(beanClass.getSimpleName()), beanClass.getName()));

                //  2.接口用实现类代替
                for (Class<?> inter : beanClass.getInterfaces()) {
                    result.add(createBeanDefinition(inter.getName(), beanClass.getName()));
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 封装 beanDefinition 对象
     * @param factoryName
     * @param className
     * @return
     */
    private BODBeanDefinition createBeanDefinition(String factoryName, String className) {
        BODBeanDefinition definition = new BODBeanDefinition();
        definition.setFactoryBeanName(factoryName);
        definition.setBeanClassName(className);
        return definition;
    }

    /**
     * 扫描包路径下的所有类保存
     * @param packageName
     */
    private void scanPackage(String packageName) {
        URL url = BODBeanDefinitionReader.class.getClassLoader().getResource("/" + packageName.replaceAll("\\.", "/"));
        File file = new File(url.getFile());
        for (File path : file.listFiles()) {
            // 包继续遍历
            if (path.isDirectory()) {
                scanPackage(packageName + "." + path.getName());
            } else {
                // 非字节文件不进行处理
                if (!path.getName().endsWith("class")) {
                    continue;
                }
                String className = packageName + "." + path.getName().substring(0, path.getName().lastIndexOf(".class"));
                this.registerBeanClasses.add(className);
            }
        }
    }

    /**
     * 获取配置值
     * @param parameter
     * @return
     */
    public String getConfig(String parameter) {
        return this.contextConfig.getProperty(parameter);
    }
}
